/**
 * 
 */
package com.gosophia.conference.action.starting;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.gosophia.commons.web.action.BaseSupportAction;
import com.gosophia.conferenceManagement.ConferenceConfiguration;
import com.gosophia.conferenceManagement.starting.dto.ConferenceStartingDTO;
import com.gosophia.conferenceManagement.starting.ws.ConferenceTransientStoreWebService;
import com.gosophia.oa.action.JudgeCurrentUndertaker;
import com.gosophia.springsecurity.UserDetailsImpl;
import com.gosophia.taskManagement.dto.JobDTO;
import com.gosophia.taskManagement.ws.TaskManagementWebService;

/**
 * 功能：点击会议申请按钮
 * 
 * @author tianchunyu
 * @since 2011-3-23 Copyright (c) 2010 Gosophia corporation All Rights Reserved
 */
@Controller("EnterStartingWithApplicationAction")
@Scope("prototype")
public class EnterStartingWithApplicationAction extends BaseSupportAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final Logger logger = Logger.getLogger(this.getClass());

	@Autowired
	private ConferenceTransientStoreWebService conferenceTransientStoreWebService;
	@Autowired
	private TaskManagementWebService taskManagementWebService;
	@Autowired
	private JudgeCurrentUndertaker judgeCurrentUndertaker;
	private ConferenceStartingDTO conferenceStartingDTO;
	private Long jobId;
	private boolean hasTransientStore;
	private boolean backWay;

	@Override
	public String _execute() throws Exception {
		// 得到当前用户
		UserDetailsImpl userDetails = (UserDetailsImpl) SecurityContextHolder
				.getContext().getAuthentication().getPrincipal();
		Long creatorId = userDetails.getUserAccountDTO().getUserInfoDTO()
				.getUserInfoId();
		if (conferenceStartingDTO != null
				&& conferenceStartingDTO.getJobId() != null) {
			judgeCurrentUndertaker
					.judgeOperationAuthority(conferenceStartingDTO.getJobId());
		}
		if (jobId != null) {
			conferenceStartingDTO = conferenceTransientStoreWebService
					.findTransientInfoByJobId(jobId);
			if (conferenceStartingDTO != null) {
				logger.debug("读取到的临时存储文件:" + conferenceStartingDTO.toString());
				hasTransientStore = true;
				JobDTO jobDto = taskManagementWebService
						.findJobDetailByJobId(jobId);
				conferenceStartingDTO.setJobId(jobId);
				conferenceStartingDTO.setConferenceCreatorId(creatorId);
				conferenceStartingDTO.setConferenceModel(ConferenceConfiguration.NEED_APPLICATION_CONFERENCE);
				conferenceStartingDTO.setConferenceName(jobDto.getMissionDTO()
						.getMissionName());
				conferenceStartingDTO.setImportentLevel(jobDto.getMissionDTO()
						.getImportanceDTO().getDataValueId());
				conferenceStartingDTO.setPlanEndTime(jobDto.getMissionDTO()
						.getScheduleEndDate());
				conferenceStartingDTO.setConferenceDescription(jobDto
						.getMissionDTO().getDescription());
				conferenceStartingDTO = conferenceTransientStoreWebService
						.updateTransientInfo(conferenceStartingDTO);

			} else {
				logger.debug("无临时存储文件");
				hasTransientStore = false;
				conferenceStartingDTO = new ConferenceStartingDTO();
				JobDTO jobDto = taskManagementWebService
						.findJobDetailByJobId(jobId);
				conferenceStartingDTO.setJobId(jobId);
				conferenceStartingDTO.setConferenceCreatorId(creatorId);
				conferenceStartingDTO.setConferenceModel(ConferenceConfiguration.NEED_APPLICATION_CONFERENCE);
				conferenceStartingDTO.setConferenceName(jobDto.getMissionDTO()
						.getMissionName());
				conferenceStartingDTO.setImportentLevel(jobDto.getMissionDTO()
						.getImportanceDTO().getDataValueId());
				conferenceStartingDTO.setPlanEndTime(jobDto.getMissionDTO()
						.getScheduleEndDate());
				conferenceStartingDTO.setConferenceDescription(jobDto
						.getMissionDTO().getDescription());
				conferenceStartingDTO = conferenceTransientStoreWebService
						.saveTransientInfo(conferenceStartingDTO);
			}

		} else {
			conferenceStartingDTO = conferenceTransientStoreWebService
					.findTransientInfoByCreator(creatorId);
			if (conferenceStartingDTO == null) {
				logger.debug("无临时存储文件");
				hasTransientStore = false;
			} else {
				logger.debug("读取到的临时存储文件:" + conferenceStartingDTO.toString());
				hasTransientStore = true;
			}
			backWay = false;
		}
		return SUCCESS;
	}

	public ConferenceStartingDTO getConferenceStartingDTO() {
		return conferenceStartingDTO;
	}

	public void setConferenceStartingDTO(
			ConferenceStartingDTO conferenceStartingDTO) {
		this.conferenceStartingDTO = conferenceStartingDTO;
	}

	public boolean isHasTransientStore() {
		return hasTransientStore;
	}

	public void setHasTransientStore(boolean hasTransientStore) {
		this.hasTransientStore = hasTransientStore;
	}

	public boolean isBackWay() {
		return backWay;
	}

	public void setBackWay(boolean backWay) {
		this.backWay = backWay;
	}

	public Long getJobId() {
		return jobId;
	}

	public void setJobId(Long jobId) {
		this.jobId = jobId;
	}

}
